package tianxiao.web.test;

import org.springframework.stereotype.Service;
import yun.web.activerecord.Db;
import yun.web.activerecord.Page;
import yun.web.activerecord.Record;
import yun.web.activerecord.tx.Tx;
import yun.web.cache.Cache;
import yun.web.enjoy.kit.Kv;
import yun.web.springmvc.paramcheck.ParamCheckResult;

import java.util.Map;

@Service
public class HelloService {

    /*------------------------
          author: 张天笑
          date: 2019/3/5
          time: 1:16
       请保证校验控制器方法与checkParam的声明一致
    -------------------------*/
    public ParamCheckResult<Kv> checkParam(String name) {
        if(name==null) return new ParamCheckResult<Kv>().setFlag(false).setR(Kv.by("msg","name不能为空"));
        return null;
    }

    public Record db(String condition) {
        Record first = Db.findFirst("select 1 as a, 2 as a_b ");
        return first;
    }


    // 分页
    @Cache(group = "db", key = "#(aes(sqlId + '-'+ page + '-'+ limit + '-'+ params.toString()))")
    public Page<Record> paginate(int page, int limit, String sqlId, Map params) {
        Page<Record> paginate = Db.paginate(page, limit, Db.getSqlPara(sqlId, params));
        return paginate;
    }


    @Tx
    public Boolean dbTx(String it) {
        Db.update("delete from t_test ");
        dbTx2(it);
        return false;
    }

    @Tx
    public Boolean dbTx2(String it) {
        Db.update("delete from t_test  ");
        int i = 1 / 0;
        return false;
    }

    public void hot() {
        System.out.println("hot");
    }


}
